<?php
declare (strict_types = 1);

namespace app\controller;

use think\facade\Validate;
use think\Request;
use app\model\Role as RoleModel;
use app\model\Auth as AuthModel;

class Role extends Base
{
    /**
     * 显示资源列表
     * get
     * @return \think\Response
     */
    public function index()
    {
        $data = RoleModel::select();
        if($data->isEmpty()) return $this->create([],'无数据',204);
        return $this->create($data,'请求角色列表成功');
    }

    /**
     * 保存新建的资源
     * post
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $param = $request->param();
        $validate = Validate::rule([
            'name' => 'require|length:2,20|unique:role',
            'status' => 'require|in:0,1',
            'remark' => 'length:0,250'
        ]);
        if(!$validate->check($param)){
            return $this->create([],$validate->getError(),400);
        }

        $id = RoleModel::create($param)->getData('id');
        if($id){
            return $this->create($id,'添加角色成功');
        }else{
            return $this->create([],'添加角色失败',400);
        }

    }

    /**
     * 显示指定的资源
     * get
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        if(!Validate::is($id,'number')) return $this->create([],'id参数不合法！',400);

        $data = RoleModel::with('profile')->find($id);
        if($data){
            return $this->create($data,'数据请求成功');
        }else{
            return $this->create([],'无数据',204);
        }
    }

    /**
     * 保存更新的资源
     * put
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $param = $request->param();

        if(!Validate::is($id,'number')) return $this->create([],'id参数不合法！',400);

        if(1==$id){
            return  $this->create([],'操作失败，超级管理员角色不能被编辑',403);
        }

        $validate = Validate::rule([
            'name' => 'require|length:2,20|unique:role',
            'status' => 'require|in:0,1',
            'remark' => 'length:1,250'
        ]);
        if(!$validate->check($param)){
            return $this->create([],$validate->getError(),400);
        }
        $group_model = RoleModel::find($id);
        if(!$group_model) return  $this->create([],'修改失败,角色不存在',400);

        if($group_model->save($param)){
            return $this->create($id,'修改角色成功');
        }else{
            return $this->create([],'修改角色失败',400);
        }
    }

    /**
     * 修改状态 put
     * @param int $id
     * @param int $state
     * @return \think\Response
     */
    public function state($id,$state){
        if(!Validate::is($id,'number') || !Validate::in($state,[0,1])){
            return  $this->create([],'参数不合法！',400);
        }
        if(1==$id){
            return  $this->create([],'操作失败，超级管理员角色不能禁用',403);
        }

        $group_model = RoleModel::find($id);
        if(!$group_model) return  $this->create([],'设置状态失败,角色不存在',400);

        if($group_model->save(['status'=>$state])){
            return  $this->create($id,'设置状态成功');
        }else{
            return  $this->create([],'设置状态失败',400);
        }
    }

    /**
     * 删除指定资源
     * delete
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        if(!Validate::is($id,'number')) return $this->create([],'id参数不合法！',400);

        if(1==$id){
            return  $this->create([],'操作失败，超级管理员角色不能删除',403);
        }

        try{
            RoleModel::find($id)->delete();
            return  $this->create([],'删除成功！');
        }catch (\Error $error){
            return  $this->create([],'错误或无数据！',400);
        }
    }

    /**
     * 分配权限
     * put
     * @param  \think\Request  $request
     * @param  int  $role_id
     * @return \think\Response
     */
    public function auth(Request $request,$role_id){
        if(!Validate::is($role_id,'number')) return $this->create([],'id参数不合法！',400);

        if(1==$role_id){
            return  $this->create([],'操作失败，超级管理员角色不能被分配权限',403);
        }

        $param = $request->param();
        $id = AuthModel::create([
                'role_id'=>$role_id,
                'smid'=>$param['smid']
            ]
            ,[],true)->getData('id');
        if($id){
            return  $this->create($id,'分配权限成功');
        }else{
            return  $this->create([],'分配权限失败',400);
        }

    }
}
